import 'package:flutter/material.dart';
import 'package:shopping_group/entity/widget_action_entity.dart';
import 'package:shopping_group/modle/more/ui/more_moneyjuan_page.dart';
import 'package:shopping_group/modle/person/bloc/tgf_money_bloc.dart';
import 'package:shopping_group/modle/person/entity/tgf_sum_entity.dart';
import 'package:shopping_group/res/colors.dart';
import 'package:shopping_group/res/dimens.dart';
import 'package:shopping_group/utils/route_util.dart';
import 'package:shopping_group/utils/widget_utils.dart';

import 'tgf_mx_money_page.dart';
import 'tgf_xjbz_money_page.dart';

class TgfMoneyPage extends StatefulWidget {
  @override
  _TgfMoneyPageState createState() => _TgfMoneyPageState();
}

class _TgfMoneyPageState extends State<TgfMoneyPage> {
  List<Widget> listViews = [
    TgfMxMoneyPage(),
    TgfXjbzMoneyPage(),
  ];

  List<String> listStr = [
    '市级代理明细',
    '县级代理明细',
    
  ];
  int _index = 0;
  List<WidgetActionEntity> _actionList = [];

  TgfSumEntity _entity = TgfSumEntity();
  TgfMoneyBloc _bloc;

  @override
  void initState() {
    super.initState();
    _bloc = TgfMoneyBloc();
    _bloc.initDate.add(null);
    _actionList
      ..add(WidgetActionEntity()
        ..actionType = 'text'
        ..title = '转代理券'
        ..titleSize = Dimens.FONT_SIZE_14
        ..actionColor = Colours.title_white);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: WidgetUtils.showAppar(context, '推广费余额',
          actionList: _actionList,
          textColor: Colors.white,
          isHide: false,
          backgroundColor: Colours.blackground_609, onTapCall: (e) {
        RouteUtil.goParameter(MoreMoneyJuanPage(
          byYonJin: 'Y',
        ));
      }),
      body: GestureDetector(
        behavior: HitTestBehavior.translucent,
        onTap: () {
          // 触摸收起键盘
          FocusScope.of(context).requestFocus(FocusNode());
        },
        child: SafeArea(
          child: Container(
            decoration: BoxDecoration(
              color: Colours.title_white,
            ),
            child: Column(
              mainAxisAlignment: MainAxisAlignment.start,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: <Widget>[
                Expanded(
                    flex: 1,
                    child: Container(
                      decoration: BoxDecoration(
                        color: Colours.blackground_609,
                      ),
                      child: Stack(
                        children: <Widget>[
                          Container(
                            alignment: Alignment.centerLeft,
                            padding: EdgeInsets.symmetric(
                                horizontal: 16, vertical: 4),
                            child: Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                _moneyView(),
                              ],
                            ),
                          ),
                        ],
                      ),
                    )),
                SizedBox(
                  height: 8,
                ),
                Expanded(
                  child: bodyView(),
                  flex: 3,
                ),
                SizedBox(
                  height: 8,
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }

  Widget _moneyView() {
    return StreamBuilder(
      stream: _bloc.sendAllData,
      initialData: _entity,
      builder: (BuildContext contexts, AsyncSnapshot<Object> snapshot) {
        Object object = snapshot.data;
        if (object is TgfSumEntity) {
          _entity = object;
        }
        return Expanded(
            child: Column(
          mainAxisAlignment: MainAxisAlignment.start,
          children: <Widget>[
            Container(
              alignment: Alignment.centerLeft,
              child: WidgetUtils.getTextColorLineView(
                  '剩余推广费：${_entity.integralAccount}',
                  Colours.title_white,
                  Dimens.FONT_SIZE_16),
            ),
            Expanded(
              child: Container(
                alignment: Alignment.center,
                child: WidgetUtils.getTextColorView(
                    '推广总收入\n${_entity.allMyGet}',
                    Colours.title_white,
                    Dimens.FONT_SIZE_22,
                    textAlign: TextAlign.center),
              ),
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                Column(
                  mainAxisAlignment: MainAxisAlignment.start,
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: <Widget>[
                    WidgetUtils.getTextColorView(
                      '市级代理',
                      Colours.title_white,
                      Dimens.FONT_SIZE_14,
                    ),
                    WidgetUtils.getTextColorView(
                      '(${_entity.myGet})',
                      Colours.title_white,
                      Dimens.FONT_SIZE_14,
                    ),
                  ],
                ),
                Column(
                  mainAxisAlignment: MainAxisAlignment.start,
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: <Widget>[
                    WidgetUtils.getTextColorView(
                      '县级代理',
                      Colours.title_white,
                      Dimens.FONT_SIZE_14,
                    ),
                    WidgetUtils.getTextColorView(
                      '(${_entity.downCustGet})',
                      Colours.title_white,
                      Dimens.FONT_SIZE_14,
                    ),
                  ],
                ),
              ],
            ),
          ],
        ));
      },
    );
  }

  Widget bodyView() {
    return Container(
        color: Colours.transparent,
        padding: EdgeInsets.symmetric(horizontal: 16, vertical: 8),
        child: Flex(
          direction: Axis.vertical,
          children: <Widget>[
            changeBottomView(),
            Expanded(
              child: listViews[_index],
            ),
          ],
        ));
  }

  Widget changeBottomView() {
    return Container(
      color: Colors.white,
      alignment: Alignment.center,
      padding: const EdgeInsets.only(top: 12),
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.center,
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          Expanded(
            child: Row(
              crossAxisAlignment: CrossAxisAlignment.center,
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                showChangeView(
                  0,
                  listStr[0],
                  _index == 0 ? Colours.blackground_609 : Colours.text_2222,
                  _index == 0 ? Colours.blackground_609 : Colors.transparent,
                ),
                showChangeView(
                  1,
                  listStr[1],
                  _index == 1 ? Colours.blackground_609 : Colours.text_2222,
                  _index == 1 ? Colours.blackground_609 : Colors.transparent,
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }

  Widget showChangeView(
      int index, String title, Color color, Color dividerColor) {
    return Expanded(
      child: GestureDetector(
        onTap: () {
          setState(() {
            _index = index;
          });
        },
        child: Container(
          alignment: Alignment.center,
          color: Colors.transparent,
          child: Stack(
            children: <Widget>[
              Column(
                children: <Widget>[
                  WidgetUtils.getTextColorView(
                      title, color, Dimens.FONT_SIZE_14),
                  Container(
                    padding: const EdgeInsets.only(top: 12),
                    //margin: EdgeInsets.symmetric(horizontal: 56),
                    width: 64,
                    child: WidgetUtils.drawCodeDivider(
                        color: dividerColor, height: 2),
                  ),
                ],
              ),
            ],
          ),
        ),
      ),
    );
  }

  @override
  void dispose() {
    super.dispose();
  }
}
